package it.java.giw.dataextraction.aleph.wrapper.filmit;

import it.java.giw.dataextraction.aleph.helper.CSVHelper;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomAttr;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class FilmitWrapperWorker implements Runnable {

	private LinkedBlockingQueue<String> datasetFiles;
	private final CountDownLatch startSignal;
	private final CountDownLatch doneSignal;
	private HashMap<String, String> attr2xpath;

	private List<String[]> results;
	private CSVHelper helper;

	/*EXAMPLES
    System.out.println(page.getTitleText());
    System.out.println(page.getHtmlElementById("footer"));
    //System.out.println(page.getAnchorByName("anchor_name"));

    //get list of all divs
    System.out.println(page.getByXPath("//div"));
    final String pageAsXml = page.asXml();
    System.out.println(pageAsXml.contains("<body class=\"composite\">"));
    final String pageAsText = page.asText();
    System.out.println(pageAsText.contains("Support for the HTTP and HTTPS protocols"));
	 */

	public FilmitWrapperWorker(LinkedBlockingQueue<String> datasetFiles, CountDownLatch startSignal, CountDownLatch doneSignal, CSVHelper helper) {
		this.datasetFiles = datasetFiles;
		this.startSignal = startSignal;
		this.doneSignal = doneSignal;
		this.results = new ArrayList<String[]>();
		this.attr2xpath = new HashMap<String,String>();
		this.helper = helper;
		initMap();
	}

	private void initMap() {
		/* xpaths di mymovies */
		attr2xpath = new HashMap<String, String>();
		attr2xpath.put("title", "/html/body/div[@id='contenitore']/div[@id='content']/div[@id='contenuti']/div[@id='colonna_sx']/div[@id='c104']/div[@class='tx-cercacinema-pi1']/div/div[@class='titolo_schedafilm']/h1/text()");
		attr2xpath.put("directors", "/html/body/div[@id='contenitore']/div[@id='content']/div[@id='contenuti']/div[@id='colonna_sx']/div[@id='c104']/div[@class='tx-cercacinema-pi1']/div/div[@id='scheda']/div[@class='schedafilm']/div[@class='testo_scheda'][4]/a/text()");
		attr2xpath.put("genre", "/html/body/div[@id='contenitore']/div[@id='content']/div[@id='contenuti']/div[@id='colonna_sx']/div[@id='c104']/div[@class='tx-cercacinema-pi1']/div/div[@id='scheda']/div[@class='schedafilm']/div[@class='testo_scheda'][2]/a/text()");
		attr2xpath.put("year", "/html/body/div[@id='contenitore']/div[@id='content']/div[@id='contenuti']/div[@id='colonna_sx']/div[@id='c104']/div[@class='tx-cercacinema-pi1']/div/div[@id='scheda']/div[@class='schedafilm']/div[@class='testo_scheda'][8]/text()");
		attr2xpath.put("rating", "/html/body/div[@id='contenitore']/div[@id='content']/div[@id='contenuti']/div[@id='colonna_sx']/div[@id='c104']/div[@class='tx-cercacinema-pi1']/div/div[@id='scheda']/div[@class='valutazione']/div[@class='stelle_valutazione'][1]/img/@src");
		attr2xpath.put("actors", "/html/body/div[@id='contenitore']/div[@id='content']/div[@id='contenuti']/div[@id='colonna_sx']/div[@id='c104']/div[@class='tx-cercacinema-pi1']/div/div[@id='scheda']/div[@class='schedafilm']/div[@class='testo_scheda_cast']/a/text()");
		attr2xpath.put("poster", "/html/body/div[@id='contenitore']/div[@id='content']/div[@id='contenuti']/div[@id='colonna_sx']/div[@id='c104']/div[@class='tx-cercacinema-pi1']/div/div[@id='trama']/div[@class='contenitore_trama']/div[@class='trama']/img/@src");
		attr2xpath.put("bioDirectorLink", "/html/body/div[@id='contenitore']/div[@id='content']/div[@id='contenuti']/div[@id='colonna_sx']/div[@id='c104']/div[@class='tx-cercacinema-pi1']/div/div[@id='scheda']/div[@class='schedafilm']/div[@class='testo_scheda'][4]/a/@href");

		/* fine xpaths mymovies */

	}

	public void run() {
		try {
			startSignal.await();
			while(!datasetFiles.isEmpty()){
				/* create webClient and set options to get only HTML */
				final WebClient webClient = new WebClient();
				webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
				webClient.getOptions().setCssEnabled(false);
				webClient.getOptions().setJavaScriptEnabled(false);
				webClient.getOptions().setAppletEnabled(false);

				HtmlPage page;
				try {
					String[] info = new String[10];
					for(int i = 0; i<info.length; i++)
							info[i] = "";
					String filename = datasetFiles.poll();
					page = webClient.getPage("file:///C:/Users/Roberto/giw2/AlephDataExtractor/download/"+filename);
					info[0] = filename;
					
					if(!page.getByXPath(attr2xpath.get("title")).isEmpty())
						info[1] = page.getByXPath(attr2xpath.get("title")).get(0).toString();
					for(Object s : page.getByXPath(attr2xpath.get("directors")))
						info[2] += s.toString()+";";
					for(Object s : page.getByXPath(attr2xpath.get("genre")))
						info[3] += s.toString()+",";
					if(!page.getByXPath(attr2xpath.get("year")).isEmpty());
						info[4] = page.getByXPath(attr2xpath.get("year")).get(0).toString();
					if(!page.getByXPath(attr2xpath.get("rating")).isEmpty()){
						DomAttr rating = (DomAttr) page.getByXPath(attr2xpath.get("rating")).get(0);
						info[5] = rating.getValue();
					}
					for(Object s : page.getByXPath(attr2xpath.get("actors")))
						info[6] += s.toString()+",";
					if(!page.getByXPath(attr2xpath.get("poster")).isEmpty()){
						DomAttr poster = (DomAttr) page.getByXPath(attr2xpath.get("poster")).get(0);
						info[7] = poster.getValue();
					}
					if(!page.getByXPath(attr2xpath.get("bioDirectorLink")).isEmpty()){
						DomAttr bioDirectorLink = (DomAttr) page.getByXPath(attr2xpath.get("bioDirectorLink")).get(0);
						info[8] = bioDirectorLink.getValue();
					}
					
					results.add(info);

				} catch (FailingHttpStatusCodeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				catch (IndexOutOfBoundsException e){
					System.err.println("Errore parsing");
				}
			}
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			doneSignal.countDown();
		}finally{
			try {
				for(String[] row : results)
					helper .writeCSV(row,"Collection FilmIt.csv");
			} catch (Exception e) {
				e.printStackTrace();
			}
			doneSignal.countDown();
		}
	}
}
